package com.study;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

/**
 * @Description Redis6-Redis秒杀案例-Redis接口类
 * @auther jianghao
 * @date 2022-04-17
 */
public class SecKillServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public SecKillServlet() {
        super();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	// 通过随机数生成用户id
        String userid = new Random().nextInt(50000) + "";
        // 接收调用的商品id
        String prodid = request.getParameter("prodid");
        // 调用秒杀方法
        // boolean isSuccess = SecKill_redis.doSecKill(userid, prodid);
        // 调用LUA脚本中的秒杀方法
        boolean isSuccess= SecKill_redisByScript.doSecKill(userid,prodid);

        // 根据秒杀的返回结果返回相应信息
        response.getWriter().print(isSuccess);
    }
}
